package com._520it.crm.service.impl;

import com._520it.crm.domain.CarInfo;
import com._520it.crm.domain.ClientCarInfo;
import com._520it.crm.mapper.ClientCarInfoMapper;
import com._520it.crm.mapper.OrderBillMapper;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.QueryObject;
import com._520it.crm.service.IClientCarInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * Created by 呆 on 2017/10/3.
 */
@Service
public class ClientCarInfoServiceImpl implements IClientCarInfoService {
    @Autowired
    private ClientCarInfoMapper clientCarInfoMapper;
    @Autowired
    private OrderBillMapper orderBillMapper;
    @Override
    public int deleteByPrimaryKey(Long id) {
        return clientCarInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ClientCarInfo clientCarInfo) {
        return clientCarInfoMapper.insert(clientCarInfo);
    }

    @Override
    public ClientCarInfo selectByPrimaryKey(Long id) {
        return clientCarInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ClientCarInfo> selectAll() {
        return clientCarInfoMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(ClientCarInfo clientCarInfo) {
        return clientCarInfoMapper.updateByPrimaryKey(clientCarInfo);
    }

    @Override
    public PageResult query(QueryObject qo) {
        Integer totalCount = clientCarInfoMapper.queryforCount(qo);
        if (totalCount==0){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<ClientCarInfo> data = clientCarInfoMapper.queryforList(qo);
        return new PageResult(totalCount,data);
    }

    @Override
    public void saveClientCarInfo(ClientCarInfo clientCarInfo, Long orderBillId, CarInfo carInfo) {
        clientCarInfo.setQueryNumber(carInfo.getMainChassis());
        clientCarInfoMapper.insert(clientCarInfo);
        //将客户车辆信息的id跟新到 保单上
        orderBillMapper.updateClientCarInfo(orderBillId,clientCarInfo.getId());
    }

    @Override
    public void updateClientCarInfo(ClientCarInfo clientCarInfo, CarInfo carInfo) {
        clientCarInfo.setQueryNumber(carInfo.getMainChassis());
        clientCarInfoMapper.updateByPrimaryKey(clientCarInfo);
    }


}
